<?php
  
/**
 * acesso a dados
 * 
 * @package 	db
 * @author 		Bruno Maciel <bruno_janga@hotmail.com>, <contato@brunomaciel.com>
 * @copyright	Copyright (C) 2009 NUTES
 * @license		http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @version 	$Id: 06/01/2010
 * 
 */
class mysql{
	
	var $DB_SERVER_HOST		=	"";
	var $DB_SERVER_USER		=	"";	
	var $DB_SERVER_PASSWORD	=	"";		
	var $DB_SERVER_DATABASE	=	"";
	var $last_insert_id=0;
	
	/**
	 * ultimo insert
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2013/03/08
	 * 
	 * 
	 * @return array
	 */	
	function getLastInsertId()
	{
		
		return $this->last_insert_id;
			
	}
	
	
	
	/**
	 * Responsavel por execultar as instrucoes SQL
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $sql
	 * 
	 * @return array
	 */	
	function setQuery($sql="")
	{
		$result		=	array();
		$array_item	=	array();
		
		//$con = mysql_connect("db-server:3307","star","st@r");
		//mysql_select_db("site_nutes_joomla15",$con);
		
		$con_ = mysql_pconnect($this->DB_SERVER_HOST,
								$this->DB_SERVER_USER,
								$this->DB_SERVER_PASSWORD);
								
		mysql_select_db($this->DB_SERVER_DATABASE,$con_);		
		
		//mysql_query("SET NAMES 'utf8'");
		//mysql_query('SET character_set_connection=utf8');
		//mysql_query('SET character_set_client=utf8');
		//mysql_query('SET character_set_results=utf8');


		$query_result = mysql_query($sql, $con_);
		
		$this->last_insert_id	=	mysql_insert_id();

		mysql_close( $con_ );
		
		return $query_result;
			
	}
	
	
		
	/**
	 * Responsavel por execultar as instrucoes SQL
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $sql
	 * 
	 * @return array
	 */	
	function getQuery($sql="")
	{
		$result		=	array();
		$array_item	=	array();
		
		//$con = mysql_connect("db-server:3307","star","st@r");
		//mysql_select_db("site_nutes_joomla15",$con);
		
		$con_ = mysql_pconnect($this->DB_SERVER_HOST,
								$this->DB_SERVER_USER,
								$this->DB_SERVER_PASSWORD);
		
		if (!$con_) {
		    die('Não foi possível conectar: ' . mysql_error());
		}
							
		mysql_select_db($this->DB_SERVER_DATABASE,$con_);
		
		
		mysql_query("SET NAMES 'utf8'", $con_);
		mysql_query('SET character_set_connection=utf8', $con_);
		mysql_query('SET character_set_client=utf8', $con_);
		mysql_query('SET character_set_results=utf8', $con_);



		



		$query_result = mysql_query($sql, $con_);
	
	
		// if (mysql_num_rows($query_result) > 0) {
		    // while ($row = mysql_fetch_assoc($query_result)) {
		       // // print_r($row);
				// array_push($result,$row);
		    // }
		// }
		
		
		
		while($row = mysql_fetch_object($query_result)){
			
			//$array_item=	array();
			//foreach($row as $key=>$item){
			//	$array_ = array(["$key"]=>$item);
			//	array_push($array_item,$array_);			
			//}
			//array_push($result,$array_item);
			array_push($result,$row);
		}
		
//		if( empty( $result ) ){
//						
//			$i = 0;
//			while ($i < mysql_num_fields($query_result)) {
//	    		$meta = mysql_fetch_field($query_result, $i);
//	    		//var_dump( $meta->name );
//	    		
//	    		array_push($result,"$meta->name" );
//	    		$i++;
//			}
//    
//		}
		
		
		mysql_free_result($query_result);
		
		mysql_close( $con_ );
				
		return $result;
			
	}
		
	
 
	/**
	 * Construtor da classe
	 * 
	 * @author Bruno Maciel<bruno_janga@hotmail.com>
	 * @version $Id 1.0 2010/01/06
	 * 
	 * @param string $host
	 * @param string $user
	 * @param string $pws
	 * @param string $db
	 * 
	 * @return void
	 */	
	function __construct($host="localhost",$user="root",$pws="",$db="")
	{

		$this->DB_SERVER_HOST		=	$host;
		$this->DB_SERVER_USER		=	$user;
		$this->DB_SERVER_PASSWORD	=	$pws;
		$this->DB_SERVER_DATABASE	=	$db;
				
	}
	
		
}

?>
